package com.ld.poetry.redis;

import javax.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * created by vital on 2024/10/7 下午2:38
 */
@Service
public class RedisStringService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 设置字符串
     * @param key
     * @param value
     */
    public void setString(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 获取字符串
     * @param key
     * @return
     */
    public Object getString(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    /**
     * 设置字符串并设置过期时间
     * @param key
     * @param value
     * @param timeout
     * @param timeUnit
     */
    public void setStringWithExpire(String key, Object value, long timeout, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    /**
     * 设置过期时间
     * @param key
     * @param timeout
     * @param timeUnit
     */
    public void setExpires(String key, long timeout, TimeUnit timeUnit){
        redisTemplate.expire(key, timeout, timeUnit);
    }

    /**
     * 删除字符串
     * @param key
     */
    public void deleteString(String key) {
        redisTemplate.delete(key);
    }

    /**
     * 判断是否存在
     * @param key
     * @return Boolean
     */
    public Boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * 自增
     * @param key
     * @return
     */
    public Long increment(String key) {
        return redisTemplate.opsForValue().increment(key);
    }


}
